package com.goldcn.sms.controller;

import com.goldcn.common.CommonUtils;
import com.goldcn.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;

/**
 * Created by kylin.woo on 3/11/2016.
 */
@Api(value = "短信中心API V1.0")
@RestController
public class SmsController {

    @Autowired
    SmsService smsService;


    @Autowired
    private JavaMailSender mailSender;

    @ApiOperation(value = "获取验证码")
    @RequestMapping(value = "/sms/{mobile}", method = RequestMethod.GET)
    public Object sendSms(@PathVariable String mobile, HttpServletRequest request) throws Exception {
        return smsService.sendSms(mobile, request);
    }


    @ApiOperation(value = "验证验证码")
    @RequestMapping(value = "/code/{mobile}", method = RequestMethod.GET)
    public Object getCode(@PathVariable String mobile, HttpServletRequest request) throws Exception {
        return smsService.getCode(mobile);
    }

    @ApiOperation(value = "银行卡4要素验证,{name,card,mobile,idno}即姓名-卡号-手机-身份证号")
    @RequestMapping(value = "/bankAuthenticate4", method = RequestMethod.POST)
    public Object verfiyBankCard4(@RequestBody JSONObject json) throws Exception {
        String name = json.optString("name", "");
        String card = json.optString("card", "");
        String mobile = json.optString("mobile", "");
        String idNo = json.optString("idno", "");
        return smsService.aliVerifyBankCard(name, card, idNo, mobile);
    }


    @ApiOperation(value = "按照指定的模版类型发送通知型短信,参数json：｛mobile:'xxxx',tempid:'xxx',param:['a,b,c...']｝")
    @RequestMapping(value = "/notice", method = RequestMethod.POST)
    public Object sengNoticeSMS(@RequestBody JSONObject json, HttpServletRequest request) throws Exception {
        String tele = json.optString("mobile", "");
        String type = json.optString("type", "");
        JSONArray paraValus = json.optJSONArray("param");
        String[] arr = new String[paraValus.size()];
        for (int i = 0; i < paraValus.size(); i++) {
            arr[i] = paraValus.getString(i);
        }
        return smsService.aliSendNoticeSMS(tele, type, arr);
    }

    @ApiOperation(value = "send simple email ,参数json：｛mobile:'xxxx',tempid:'xxx',param:['a,b,c...']｝")
    @RequestMapping(value = "/simpleEmail", method = RequestMethod.POST)
    public void sendSimpleEmail(@RequestBody JSONObject json) {
        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom("it@gcn99.com");//发送者.
        message.setTo("13798185456@139.com");//接收者.
        message.setSubject("测试邮件（邮件主题）");//邮件主题.
        message.setText("这是邮件内容");//邮件内容.

        mailSender.send(message);//发送邮件
    }


    @ApiOperation(value = "send attachment email ,参数json：｛mobile:'xxxx',tempid:'xxx',param:['a,b,c...']｝")
    @RequestMapping(value = "/sendAttachmentsEmail", method = RequestMethod.POST)
    public void sendAttachmentsEmail(@RequestBody JSONObject json) throws Exception {
        //这个是javax.mail.internet.MimeMessage下的，不要搞错了。
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        //基本设置.
        helper.setFrom("412887952@qq.com");//发送者.
        helper.setTo("1473773560@qq.com");//接收者.
        helper.setSubject("测试附件（邮件主题）");//邮件主题.
        helper.setText("这是邮件内容（有附件哦.）");//邮件内容.

        //org.springframework.core.io.FileSystemResource下的:
        //附件1,获取文件对象.
        FileSystemResource file1 = new FileSystemResource(new File("D:/test/head/head1.jpg"));
        //添加附件，这里第一个参数是在邮件中显示的名称，也可以直接是head.jpg，但是一定要有文件后缀，不然就无法显示图片了。
        helper.addAttachment("头像1.jpg", file1);
        //附件2
        FileSystemResource file2 = new FileSystemResource(new File("D:/test/head/head2.jpg"));
        helper.addAttachment("头像2.jpg", file2);

        mailSender.send(mimeMessage);
    }


    @ApiOperation(value = "sendInlineMail ,参数json：｛mobile:'xxxx',tempid:'xxx',param:['a,b,c...']｝")
    @RequestMapping(value = "/sendInlineMail", method = RequestMethod.POST)
    public void sendInlineMail(@RequestBody JSONObject json) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //基本设置.
        helper.setFrom("412887952@qq.com");//发送者.
        helper.setTo("1473773560@qq.com");//接收者.
        helper.setSubject("测试静态资源（邮件主题）");//邮件主题.
        // 邮件内容，第二个参数指定发送的是HTML格式
        //说明：嵌入图片<img src='cid:head'/>，其中cid:是固定的写法，而aaa是一个contentId。
        helper.setText("<body>这是图片：<imgsrc='cid:head' /></body>", true);

        FileSystemResource file = new FileSystemResource(new File("D:/test/head/head1.jpg"));
        helper.addInline("head", file);

        mailSender.send(mimeMessage);
    }


    @ApiOperation(value = "分页查询短信息,参数json：｛companyID:'xxxx',type:'xxx',mobile:'XXX',smsTemplateCode:'XXX',smsFreeSignName:'XXX',benginTime:'XXX',endTime:'XXX'｝")
    @RequestMapping(value = "/querySms", method = RequestMethod.POST)
    public Object querySms(HttpServletRequest request) throws Exception {
        Map<String, String> params = CommonUtils.getParameterMap(request);
        return smsService.querySms(params);
    }


}
